/**
 * Created by sun on 2016/8/4.
 */
define(function (require) {
    var app = require('/../js/app');
    require('ui-table');

    require('ueditor.all.min');
    require('ueditor.config');
    require('angular-ueditor');
    require('upload');
    app.useModule("ui.table");
    app.useModule("ng.ueditor");
    app.useModule("ngFileUpload");
    var datepicker = require('datepicker');
    var toastr= require('toastr');
    app.controller('articleCtrl', ['$scope','$http','$filter','Upload' ,function ($scope, $http,$filter,Upload) {
        $scope.orderParams = {
            url: "/admin/article/list"
        };
        datepicker($scope);
        $scope.add={};

        //创建
        $scope.addOrderModal=function(){
            $scope.addModal = !$scope.addModal ;
            $scope.add = {};
            $scope.pics=  {};
            $scope.imgs=[];
        };
        //全局查询
        $scope.search = function() {
            $scope.ctrlScope.itemsearch.beginDateTime=$scope.ctrlScope.itemsearch.beginDateTimex;
            $scope.ctrlScope.itemsearch.endDateTime=$scope.ctrlScope.itemsearch.endDateTimex;
            $scope.ctrlScope.itemsearch.beginDateTime=$filter('date')($scope.ctrlScope.itemsearch.beginDateTime,'yyyy-MM-dd');
            $scope.ctrlScope.itemsearch.endDateTime=$filter('date')($scope.ctrlScope.itemsearch.endDateTime,'yyyy-MM-dd');
            //表格查询参数设置
            $scope.orderParams.ajaxParam=$scope.ctrlScope.itemsearch;
            //表格刷新
            $scope.orderParams.reload();
        };
        //全局查询重置
        $scope.reset=function(){
            $scope.orderParams.ajaxParam={};
            $scope.ctrlScope.itemsearch={};
            $scope.orderParams.reload();
        };

        //新建
        $scope.addBtn = function(){
            console.info($scope.add);
            var parm=$scope.add;
            parm.pics=$scope.pics;
            parm.userId=1;
            console.info(parm);
            $http({
                method: 'POST',
                url: "/admin/article/add",
                data:parm,
            }).success(function(data) {
                console.info(data);
                toastr.success("创建成功！");
                $scope.addModal = !$scope.addModal;
                $scope.orderParams.reload();
            }).error(function(data) {
            });
        };
        //编辑
        $scope.modifyModal = function(id){
            $scope.detailsModal= !$scope.detailsModal;
            $scope.getOne(id);
        };
        //删除
        $scope.delModal = function(id){
            if(confirm("确认删除此条信息?")) {
                $http({
                    method: 'POST',
                    url: "/admin/article/deleteByArticleId",
                    data: {
                        articleId:id
                    }
                }).success(function(data) {
                    if(data.result=="success"){
                        toastr.success("删除成功");
                        $scope.orderParams.reload();
                    }
                    else{
                        toastr.success("删除失败");
                    }

                }).error(function(data) {
                });
            }
            else{
                return;
            }
        };
        //单个查询
        $scope.getOne = function(id){
            $http({
                method: 'POST',
                url: "/admin/article/getSysArticleById",
                data: {
                    id:id
                }
            }).success(function(data) {
                console.info(data);
                $scope.list=data;
                $scope.rechargeStatus=data.rechargeStatus;
            }).error(function(data) {
            });
            $http({
                method: 'POST',
                url: "/admin/picture/getPictureByItemId",
                data: {
                    itemId:id
                }
            }).success(function(data) {
                console.info(data);
                $scope.imgs=[];
                $scope.imgs.push(data[0].picturePath);
            }).error(function(data) {
            });
        };
        //保存
        $scope.saveBtn = function(){
            var parm=$scope.list;
            parm.pics=$scope.pics;
            parm.hasImage=1;
            $http({
                method: 'POST',
                url: "/admin/article/modify",
                data:parm,
            }).success(function(data) {
                console.info(data);
                $scope.detailsModal= !$scope.detailsModal;
                $scope.orderParams.reload();
            }).error(function(data) {
            });
        };

        //上传图片
        $scope.selectUpload=function(){
            $scope.uploadTab.active=true;
        };

        $scope.onFileSelect = function (file) {
            console.log(file);
        };

        $scope.removeImg=function(index) {
            console.log(index);
            var imgs = $scope.imgs;
            console.log(imgs)
            imgs.splice(index,1);
            $scope.pics="undefine";
            for(var i=0;i<imgs.length;i++){
                $scope.pics=$scope.pics+"|"+imgs[i];
            }
        };

        $scope.uploadPic =function(file) {
            $scope.imgs=[];
            console.log(Upload);
            file.upload = Upload.upload({
                url: '/itemFile',
                headers: {
                    'optional-header': 'header-value'
                },
                data: {username: $scope.username, file: file}
            });
            file.upload.then(function (response) {
                $scope.pics=response.data.url;
                $scope.imgs.push(response.data.url);
                $scope.defaultPicFile="css/img/updatepic.png";
            }, function (response) {
                if (response.status > 0)
                    $scope.errorMsg = response.status + ': ' + response.data;
            }, function (evt) {
                file.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
                console.log(file.progress);
            });
            file.upload.xhr(function (xhr) {
            });
        };
    }]);


});